package com.gt.acc

import com.gt.SCUtil
import org.apache.spark.SparkContext
import org.apache.spark.rdd.RDD

object Spark_acc_02 {

  def main(args: Array[String]): Unit = {
    val sc: SparkContext = SCUtil.buildLocalSc()
    val rdd: RDD[Int] = sc.makeRDD(List(1, 2, 3, 4, 5), 3)

    val sumAcc = sc.longAccumulator("sum")
    val rdd1: RDD[Int] = rdd.map(num => {
      sumAcc.add(num)
      num
    })
    //少累加。因为没有action算子

    //多累加，action算子执行多次，map方法多次执行导致
    //rdd1.collect()
    //rdd1.collect()




    println(sumAcc.value)
    sc.stop()

  }

}
